import { BufferGeometry, Float32BufferAttribute, Material, Mesh, MeshPhongMaterial, Vector3 } from "three";

/**
 * 模型网格基类
 */
export default class ModelMesh extends Mesh {

    private _meshface:Vector3[][];

    translateVec:Vector3;

    constructor(geometry: BufferGeometry, material: Material) {

        super(geometry, material);

        this.name = 'modelmesh';


        this._meshface = [];

       
    }

    get meshface():Vector3[][] {
        return this._meshface;
    }

    /**
    * 更新几何体
    * @param vertices 
    * @param indices 
    */
    updateMoldGeometry(vertices: number[], indices: number[], uvs: number[], faceVec:Vector3[][],vecColors?: number[]) {

        this._meshface = faceVec;

        this.geometry.attributes.normal = undefined;
        this.geometry.setIndex(indices);

        this.geometry.setAttribute('position', new Float32BufferAttribute(vertices, 3));
        if (vecColors && vecColors.length == vertices.length) {
            this.geometry.setAttribute('color', new Float32BufferAttribute(vecColors, 3));
        }
        this.geometry.setAttribute('uv', new Float32BufferAttribute(uvs, 2));

        this.geometry.computeVertexNormals();

        this.updateGeometry();
    }

    /**
     * 更新顶点
     * @param vertices 
     */
    updateMoldVertices(vertices: number[]): void {
        this.geometry.attributes.normal = undefined;
        this.geometry.setAttribute('position', new Float32BufferAttribute(vertices, 3));
        this.geometry.computeVertexNormals();

        this.updateGeometry();
    }

    /**
     * 更新索引
     * @param indices 
     */
    updateMoldIndices(indices: number[]): void {
        this.geometry.attributes.normal = undefined;
        this.geometry.setIndex(indices);
        this.geometry.computeVertexNormals();

        this.updateGeometry();
    }

    dispose(): void {
        this.onDispose();
        if (this.geometry) this.geometry.dispose();
        if (this.material) (this.material as MeshPhongMaterial).dispose();
    }

    /** 子类数据销毁 */
    protected onDispose(): void {
        throw new Error("需要子类复写，用于销毁子类数据");
    }

    protected updateGeometry(): void {

    }

}